﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Manage.Blog
{
    public partial class Index :MgConfig
    {
        protected int pagesize = 10;
        protected int cpage = 1;//当前页
        protected List<int?> count;

        Jy123.Model.User_Blog modelBlog = new Jy123.Model.User_Blog();
        Jy123.BLL.User_Blog bllBlog = new Jy123.BLL.User_Blog();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Index.aspx");
            string type = Request.QueryString["Type"];
            if (type == "del")
            {
                new BLL.User_Blog().Delete(Convert.ToInt32(Request.QueryString["id"]));
                BindRpt();
            }
            else if (type == "Islock")//是否锁定
            {
                modelBlog = bllBlog.GetModel(Convert.ToInt32(Request.QueryString["id"]));
                if (int.Parse(Request.QueryString["flag"]) == 1)
                {
                    modelBlog.IsLock = true;
                    bllBlog.Update(modelBlog);
                }
                else if (int.Parse(Request.QueryString["flag"]) == 2)
                {
                    modelBlog.IsLock = false;
                    bllBlog.Update(modelBlog);
                }
            }
            else if (type == "Recommended")
            {
                modelBlog = bllBlog.GetModel(Convert.ToInt32(Request.QueryString["id"]));
                if (int.Parse(Request.QueryString["flagrec"]) == 1)
                {
                    modelBlog.isRec = 1;
                    bllBlog.Update(modelBlog);
                }
                else if (int.Parse(Request.QueryString["flagrec"]) == 2)
                {
                    modelBlog.isRec = 0;
                    bllBlog.Update(modelBlog);
                }
            }
            BindRpt();
        }
        /// <summary>
        /// RPT数据绑定
        /// </summary>
        private void BindRpt()
        {
            //if (!string.IsNullOrEmpty(Key.Text))//搜索框不为空
            //{
                string searchCondtion = string.Empty;
                string sortCondtion = string.Empty;
                if (!string.IsNullOrEmpty(Key.Text))
                {
                    if (Condtion.SelectedValue == "2")
                    {
                        searchCondtion = "Title like '%" + Key.Text + "%'";
                    }
                    else if (Condtion.SelectedValue == "1")
                    {
                        searchCondtion = "UserName like '%" + Key.Text + "%'";
                    }
                }
                switch (Order.SelectedValue)
                {
                    case "1":
                        sortCondtion = "PostTime DESC";
                        break;
                    case "2":
                        sortCondtion = "UserName";
                        break;
                    case "3":
                        sortCondtion = "Comments DESC";
                        break;
                    default:
                        sortCondtion = "PostTime DESC";
                        break;
                }
                //if(Order.SelectedValue == "1")
                //{
                //    sortCondtion = "PostTime DESC";
                //}
                //else if(Order.SelectedValue == "2")
                //{
                //    sortCondtion = "UserName";
                //}
                //else if(Order.SelectedValue == "3")
                //{
                //    sortCondtion = "Comments DESC";
                //}
                RptData.DataSource = new BLL.User.Blog().ManageBlogList(cpage, pagesize, searchCondtion,sortCondtion, out count);
                RptData.DataBind();
            //}
            //else
            //{
            //    RptData.DataSource = new BLL.User.Blog().ManageBlogList(cpage, pagesize, string.Empty,string.Empty,out count);
            //    RptData.DataBind();
            //}

        }
        ///// <summary>
        ///// 显示锁定按钮
        ///// </summary>
        ///// <param name="islock"></param>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //protected string ShowLock(object islock, object id)
        //{
        //    if (islock != null && islock != DBNull.Value && (bool)islock == true)
        //    {
        //        return "<a href=\"javascript:UnLockIt('" + id + "');\">解锁</a>";
        //    }
        //    else
        //    {
        //        return "<a href=\"javascript:LockIt('" + id + "');\">锁定</a>";
        //    }
        //}

        ///// <summary>
        ///// 显示推荐按钮
        ///// </summary>
        ///// <param name="islock"></param>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //protected string ShowRec(object isRec, object id)
        //{
        //    if (isRec != null && isRec != DBNull.Value && isRec.ToString() == "1")
        //    {
        //        return "<a href=\"MoodMng.aspx?isRec=0&id=" + id.ToString() + "\"><span class=\"Blue\">取消推荐</span></a>";
        //    }
        //    else
        //    {
        //        return "<a href=\"MoodMng.aspx?isRec=1&id=" + id.ToString() + "\">推荐</a>";
        //    }
        //}

        /// <summary>
        /// 显示状态
        /// </summary>
        /// <param name="islock"></param>
        /// <returns></returns>
        protected string ShowLock(object islock)
        {
            if (islock != null && islock != DBNull.Value && (bool)islock == true)
            {
                return "<a class=\"cred\">锁定</a>";
            }
            else
            {
                return "正常";
            }
        }

        /// <summary>
        /// 显示推荐
        /// </summary>
        /// <param name="isRec">被转换前的数值</param>
        /// <returns></returns>
        protected string Rec(object isRec)
        {
            if (Convert.ToInt16(isRec) == 0)
                return "未推荐";
            else
                return "<a class=\"cred\">推荐</a>";
        }
        /// <summary>
        /// 推荐按钮文字及样式替换
        /// </summary>
        /// <param name="rec">是否推荐(绑定的值)</param>
        /// <param name="id">绑定的记录ID</param>
        /// <param name="page">页码</param>
        /// <returns>返回新按钮文字及样式</returns>
        protected string Rec(object rec, object id, object page)
        {
            if (rec != null && Convert.ToInt16(rec) == 0)
            {
                return "<a class=\"btnsty\" href=\"javascript:Recommended(" + id + ",1,'" + page + "');\">推荐</a>";
            }
            else
            {
                return "<a class=\"btnsty cred\" href=\"javascript:Recommended(" + id + ",2,'" + page + "');\">取消推荐</a>";
            }
        }
        /// <summary>
        /// 锁定按钮文字及样式替换
        /// </summary>
        /// <param name="islock">是否锁定(绑定的值)</param>
        /// <param name="id">绑定的记录ID</param>
        /// <param name="page">页码</param>
        /// <returns>返回新按钮文字及样式</returns>
        protected string Lock(object islock, object id, object page)
        {
            if (islock != null && Convert.ToInt16(islock) == 0)
            {
                return "<a class=\"btnsty cred\" href=\"javascript:Islock(" + id + ",1,'" + page + "');\">锁定</a>";
            }
            else
            {
                return "<a class=\"btnsty\" href=\"javascript:Islock(" + id + ",2,'" + page + "');\">取消锁定</a>";
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {

        }
    }
}
